package zb.demo.service.impl;


import org.springframework.stereotype.Service;
import zb.blog.base.util.PropertiesUtil;
import zb.blog.base.util.StringUtil;
import zb.demo.bean.Qrcode;
import zb.demo.service.IQrcodeService;
import zb.demo.util.QrcodeUtil;

import javax.servlet.http.HttpServletRequest;

@Service
public class QrcodeService implements IQrcodeService {
    public static final String PATH_LOC_TEM = PropertiesUtil.getPropValue("demo.fileinputupload.path");

    @Override
    public String getQrcode(Qrcode qrcode, HttpServletRequest request) {
        String logoFile = "";
        if (qrcode.getLogo() == null || qrcode.getLogo().trim().length() <= 0) {
            logoFile = null;
        } else {
            String ip = StringUtil.getIpAddr(request);
            logoFile = qrcode.getLogo().substring(qrcode.getLogo().lastIndexOf("/"));
            logoFile = PATH_LOC_TEM + ip + logoFile;
        }
        String content = "BEGIN:VCARD\n" +
                "VERSION:3.0\n" +
                "FN:" + qrcode.getName() + "\n" +
                "PHOTO;ENCODING=b;" + qrcode.getLogo() + "\n" +
                "TEL;CELL;VOICE:" + qrcode.getTel() + "\n" +
                "TEL;WORK;VOICE:" + qrcode.getWork_tel() + "\n" +
                "EMAIL;PREF;INTERNET:" + qrcode.getEmail() + "\n" +
                "URL:" + qrcode.getUrl() + "\n" +
                "ADR;WORK;POSTAL:" + qrcode.getAddr() + ";" + qrcode.getCompany() + "\n" +
                "ROLE:" + qrcode.getRole() + "\n" +
                "TITLE:" + qrcode.getTitle() + "\n" +
                "END:VCARD";
        return QrcodeUtil.getQrcode(logoFile, content);
    }
}
